﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DoeObjects;
using DoeLibsMVC.Security;
using DoeLibsMVC;

namespace DoeLibsMVC.Views
{
    public abstract class BaseViewPage : WebViewPage
    {
        public virtual new CustomPrincipal User
        {
            get
            {
                return base.User as CustomPrincipal;
            }
        }

        /// <summary>
        /// field which contains the message.
        /// </summary>
        public HttpMessage Message
        {
            get
            {
                if (Request.Cookies.AllKeys.Contains(HttpMessage.MESSAGE_COOKIE_NAME) && !string.IsNullOrEmpty(Request.Cookies[HttpMessage.MESSAGE_COOKIE_NAME].Value))
                {
                    HttpMessage msg = HttpMessage.getFromJson(Request.Cookies[HttpMessage.MESSAGE_COOKIE_NAME].Value);

                    return msg;
                }
                return null;
            }
            set
            {
                if (value == null)
                {
                    //expire old cookie (-->delete message)
                    HttpCookie cookie = new HttpCookie(HttpMessage.MESSAGE_COOKIE_NAME);
                    cookie.Expires = DateTime.Now.AddDays(-1);
                    Response.Cookies.Add(cookie);
                }
                else
                {
                    HttpCookie cookie = new HttpCookie(HttpMessage.MESSAGE_COOKIE_NAME);
                    cookie.Expires = DateTime.Now.AddDays(1);
                    cookie.Value = value.getJson();
                    Response.Cookies.Add(cookie);
                }
            }
        }
    }
    public abstract class BaseViewPage<TModel> : WebViewPage<TModel>
    {
        public virtual new CustomPrincipal User
        {
            get
            {
                return base.User as CustomPrincipal;
            }
        }

        /// <summary>
        /// field which contains the message.
        /// </summary>
        public HttpMessage Message
        {
            get
            {
                if (Request.Cookies.AllKeys.Contains(HttpMessage.MESSAGE_COOKIE_NAME) && !string.IsNullOrEmpty(Request.Cookies[HttpMessage.MESSAGE_COOKIE_NAME].Value))
                {
                    HttpMessage msg = HttpMessage.getFromJson(Request.Cookies[HttpMessage.MESSAGE_COOKIE_NAME].Value);

                    return msg;
                }
                return null;
            }
            set
            {
                if (value == null)
                {
                    //expire old cookie (-->delete message)
                    HttpCookie cookie = new HttpCookie(HttpMessage.MESSAGE_COOKIE_NAME);
                    cookie.Expires = DateTime.Now.AddDays(-1);
                    Response.Cookies.Add(cookie);
                }
                else
                {
                    HttpCookie cookie = new HttpCookie(HttpMessage.MESSAGE_COOKIE_NAME);
                    cookie.Expires = DateTime.Now.AddDays(1);
                    cookie.Value = value.getJson();
                    Response.Cookies.Add(cookie);
                }
            }
        }
    }
}